package com.kfpanda.spring.batch.listener;

import java.util.List;

import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.listener.StepListenerSupport;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.kfpanda.search.index.entity.InfoEntity;
import com.kfpanda.spring.batch.incrementer.Incrementer;

@Scope("step")
@Repository("infosStepListener")
@SuppressWarnings("rawtypes")
public class InfosStepListener extends StepListenerSupport implements StepListener{
	
	protected StepExecution stepExecution;
	
	@Override
	public void beforeStep(StepExecution stepExecution) {
		this.stepExecution = stepExecution;
	}
	
	@Override
	public void afterWrite(List items) {
		ExecutionContext ec = this.stepExecution.getJobExecution().getExecutionContext();
		ec.put(Incrementer.INCREMENTER_NAME, 
				( (InfoEntity)items.get(items.size() - 1) ).getInfoId() );
//		this.stepExecution.getExecutionContext().put(Incrementer.INCREMENTER_NAME, 
//				( (InfoEntity)items.get(items.size() - 1) ).getInfoId() );
	}
	
}
